package org.example.iotdb.session;

import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.session.pool.SessionPool;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Auther: kevin
 * @Description:
 * @Company: 上海博般数据技术有限公司
 * @Version: 1.0.0
 * @Date: Created in 11:36 2021/8/19
 * @ProjectName: iotdb-learn
 */
public class IotDBSessionTask {

    private SessionPool getSession() {
        String host = "10.172.246.203";
        int port = 6667;
        String user = "root";
        String password = "root";
        int poolSize = 2;
        SessionPool session = new SessionPool(host, port, user, password, poolSize);
        return session;
    }

    private Session buildSession() throws Exception{
        List<String> nodeUrls = new ArrayList<>(3);
        nodeUrls.add("10.172.246.203:6667");
        nodeUrls.add("10.172.246.204:6667");
        nodeUrls.add("10.172.246.205:6667");
        String user = "root";
        String password = "root";
        Session session = new Session(nodeUrls, user, password);
        session.open();
        return session;
    }

    public static void main(String[] args) throws Exception{
        IotDBSessionTask task = new IotDBSessionTask();
        String storageGroup = "root.t1";
        String device = storageGroup + ".m1.d3";
//        SessionPool session = task.getSession();
        Session session = task.buildSession();

        List<String> measurements = Arrays.asList("P", "Q", "I", "U");
        List<TSDataType> dataTypes = Arrays.asList(TSDataType.DOUBLE, TSDataType.DOUBLE, TSDataType.DOUBLE, TSDataType.DOUBLE);
        try {
            long start = System.currentTimeMillis();
            Random random = new Random();
            List<String> devices = new ArrayList<>();
            List<Long> times = new ArrayList<>();
            List<List<String>> measurementsList = new ArrayList<>();
            List<List<TSDataType>> datatypesList = new ArrayList<>();
            List<List<Object>> valuesList = new ArrayList<>();
            for (int i = 0; i < 10000; i++) {
                long time = start + i * 1000;
                List<Object> values = Arrays.asList(
                        task.dataFormat(random.nextDouble() * 100, 2),
                        task.dataFormat(random.nextDouble() * 100, 2),
                        task.dataFormat(random.nextDouble() * 100, 2),
                        task.dataFormat(random.nextDouble() * 100, 2)
                );
//                session.insertRecord(device, time, measurements, dataTypes, values);
                devices.add(device);
                times.add(time);
                measurementsList.add(measurements);
                datatypesList.add(dataTypes);
                valuesList.add(values);
            }
            session.insertRecords(devices, times, measurementsList, datatypesList, valuesList);
            long end = System.currentTimeMillis();
            System.out.println(String.format("duration[%s]ms", (end - start)));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                session.close();
            } catch (IoTDBConnectionException e) {
                e.printStackTrace();
            }
        }
    }

    public double dataFormat(double value, int decimals) {
        if (decimals == 0) {
            decimals = 4;
        }
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(decimals, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
